<?php

class Admin_Model_Productcategories extends Zend_Db_Table_Abstract
{
    protected $_name = 'productcategory';
    protected $_primary = 'PRODUCTCATEGORYID';
    
    public function __construct($config = array())
    {
        $this->_db = Zend_Db_Table::getDefaultAdapter();
        parent::__construct($config);
        $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH ."/log.txt");
        $this->logger = new Zend_Log($writer);
    }
    
    public function findAll($storeId,$limit, $offset){
        $data = $this->select();
        $data->from($this->_name);
        $data->where('STOREID = ', $storeId);
        $data->limit($limit,$offset);
        $data = $this->fetchAll($data);
        return $data;
    }
    
    public function findByStoreId($storeId){
        $data = $this->select();
        $data->from($this->_name);
        $data->where('STOREID = ?', $storeId);
        $data = $this->fetchAll($data);
        return $data;
    }
    
    public function addNewProductcategory($params){
        $db = $this->getDefaultAdapter();
        $data = array(
                'NAME' => $params['NAME'],
                'STOREID' => $params['STOREID'],
                'PARENTID' => $params['PARENTID']
        );
        try{
            
        }catch(Zend_Exception $e){
            echo $e->getMessage();
        }
    }
    
    public function updateProductcategory($params){
        $db = $this->getDefaultAdapter();
        $id = $params['PROCDUCTCATEGORYID'];
        $data = array();
        if(isset($params['NAME'])) $data['NAME'] = $params['NAME'];
        if(isset($params['STOREID'])) $data['STOREID'] = $params['STOREID'];
        if(isset($params['PARENTID'])) $data['PARENTID'] = $params['PARENTID'];
        try{
            $db->update($this->_name, $data, 'PROCDUCTCATEGORYID =' .$id);
        }catch(Zend_Exception $e){
            echo $e->getMessage();
            exit;
        }
    }
    
    public function count($storeId){
        $db = Zend_Db_Table::getDefaultAdapter();
        $where = "(1=1)";
        if($storeId != ""){
            $where .= " and STOREID = ".$storeId;
        }
        $sql = "select count(*) as COUNT from ".$this->_name ." P WHERE $where";
        $result = $db->query($sql)->fetch();
        return $result['COUNT'];
    }
    
    public function deleteProductcategory($id){
        $where = "PRODUCTCATEGORYID =".$id;
        $this->delete($where);
    }
    
}
?>